package bookstore.utils;

import bookstore.model.Order;
import bookstore.model.User;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.Optional;

@Service
public class SessionUtils {
    final String PROFILE = "profile";
    final String ORDER = "order";

    final public Optional<UserInfo> setProfile(HttpSession session, User user) {
        UserInfo info = new UserInfo(user.getUserid(), user.getUsername(), user.getStaff());
        session.setAttribute(PROFILE, info);
        return Optional.ofNullable((UserInfo) session.getAttribute(PROFILE));
    }

    final public Optional<UserInfo> getProfile(HttpSession session) {
        return Optional.ofNullable((UserInfo) session.getAttribute(PROFILE));
    }


    final public Optional<Order> setOrder(HttpSession session, Order order) {
        session.setAttribute(ORDER, order);
        return Optional.ofNullable((Order) session.getAttribute(ORDER));
    }

    final public Optional<Order> getOrder(HttpSession session) {
        return Optional.ofNullable((Order) session.getAttribute(ORDER));
    }
}
